package com.inventive.jmscache.server.hazelcast;

import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.IList;
import com.inventive.jmscache.server.DestinationsCache;
import com.inventive.jmscache.server.JmsCacheException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 */
@Component
public class HazelcastDestinationsCache implements DestinationsCache {

    private IList<String> queues = Hazelcast.getList("queueNames");
    private IList<String> topics = Hazelcast.getList("topicNames");

    public List<String> getQueueNames() {
        return new ArrayList<String>(queues);
    }

    public List<String> getTopicNames() {
        return new ArrayList<String>(topics);
    }

    public void addQueue(String queue) {
        queues.add(queue);
    }

    public void addTopic(String topic) {
        topics.add(topic);
    }

    public void checkContainsQueue(String name) {
        if (!queues.contains(name)) {
            throw new JmsCacheException("Queue does not exist");
        }
    }

    public void checkContainsTopic(String name) {
        if (!topics.contains(name)) {
            throw new JmsCacheException("Topic does not exist");
        }
    }

}
